# coding=UTF-8
# 字体编码,coding必须在第一行
# 01
print("Hello World")

# 02
score = 77
if score >= 80:
    print(score)
else:
    print("分数过低")

# 03
# rang(1,6), 不包含6
# for i in range(1, 6):
#     print(i ** 2)

# 04
# while True:
#     i = input("请输入内容:")
#     if i == "stop":
#         break
#     elif not i.isdigit():
#         if i.isspace():
#             print("不能输入空格")
#         else:
#             print("请输入数字")
#
#     else:
#         j = int(i)
#         if j > 20:
#             print("输入的数字太大了")
#         elif j < 10:
#             print("输入的数字太小了")
#         else:
#             print("输入正确")

# 05 循环增加
# i = 1
# while True:
#     if i >= 10:
#         print("大于10,退出")
#         break
#     else:
#         i = i + 1
#         print(i)
#         print("+1中...")

# 06 默认浏览器打开网页
# url = "https://www.baidu.com/"
# import webbrowser
#
# webbrowser.open(url)
# True

# 07 读取网页内容 python 2+版本
# import urllib2
#
# html = urllib2.urlopen(url)
# print(html.read())

# 08 访问网页
url = "https://blog.csdn.net/qq_33160790"
# import urllib.request
#
# req = urllib.request.Request(url)
# reqs = urllib.request.urlopen(req)
# data = reqs.read().decode("utf-8")
# print(data)

# 09 单独获取url,暂未成功
# from lxml import etree
# import requests
#
# req = requests.get(url)
# if req.status_code == requests.codes.ok:
#     html = etree.HTML(req.text)
#     hrefs = html.xpath('////span[@class="link_title"]/a/@href')
#     for href in hrefs:
#         print(href)
result = False
if result:
    print("true")
else:
    print("false false")

# 10 多行注释
'''
这是多行注释，使用单引号。
这是多行注释，使用单引号。
这是多行注释，使用单引号。
'''
# 11 单行显示
x = "1"
y = "2"
print(x, y)

# 12 多变量赋值
a = b = c = 1
print(a, b, c)
a, b, c = 1, 2, "雄"
print(a, b, c)

# 13 list 列表
#        0    1    2    3    4   5   6  7  8
# 列表
list = ["a", "b", "c", "d", "e", 1, 2, 2, 2]
# 数组
tuple = ("1", "2", "3", "4", "a", "b")
# 包含1, 不包含3
print(list[1:3])
print(tuple[1:3])

print(list[2:])
print(tuple[2:])
# 2出现的次数
print(list.count(2))
print(tuple.count("2"))
# position=2
print(list[2])
print(tuple[2])

#  14 字典
tinydict = {'name': 'john', 'code': 6734, 'dept': 'sales'}
print("key=", tinydict.keys())
print("value=", tinydict.values())
print(tinydict.copy())

# 15 强转
x = 1
y = 1.0
print(float(x))
print(int(y))

# 16 判断值是否被包含在内
if x in tinydict:
    print(True)
else:
    print(False)

if "code" in tinydict:
    print(True)
else:
    print(False)

# 17 身份运算符
a = 10
b = 10
if a is b:
    print(True)
else:
    print(False)

# 18 循环 break与 continue区别
for tiny in tinydict:
    print(tiny)
print("结束")
for letter in "python":
    if letter == "h":
        # 1 终止当前循环
        # break
        # 2 跳过当前值, 继续下一轮
        continue
    else:
        print(letter)

#  19 while 循环
# 倒叙, 从16开始判断
numbers = [11, 12, 13, 14, 15, 16]
even = []
odd = []
while len(numbers) > 0:
    number = numbers.pop()
    if number % 2 == 0:
        # 附加
        even.append(number)
    else:
        odd.append(number)
print("偶数", even)
print("基数", odd)

numbers1 = [11, 12, 13, 14, 15, 16]
# [-2] 读取列表中倒数第二个元素
print("-2的位置=", numbers1[-2])  # 15
print("最大值=", max(numbers1))

del numbers1[2]
print("删除一个元素后:", numbers1)

# 20 math 方法
import math

i = math.pow(2, 3)  # 2的3次方
print(i)

# 21 替换带有%s的符号
print("My name is %s and weight is %s kg! I'm %s" % ('Zara', 21, "beautiful"))

# 22  时间戳 年月日 时分秒
import time

print("当前时间为=", time.asctime(time.localtime(time.time())))
print("时间为:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# 23 打印日历
import calendar

cal = calendar.month(2018, 9)
print(cal)

import datetime

now = datetime.datetime.now()
print("现在的时间是:", now)


# 24  方法
def enter(str, str2):
    print(str, str2)
    return


enter("我要调用enter方法", "参数2")
enter(1, "参数22")

# 25 lambda
# 后面的x-y才是运算式
sum = lambda x, y, z: x - y + z;

print("lambda相加=", sum(10, 20, 11))
print("lambda相加=", sum(20, 30, 12))

money = 100


def addMoney():
    global money
    money = money + 1


print(money)
addMoney()
print(money)

# content = dir(math)
# print(content)

# 异常捕获
try:
    file1 = open("refile.txt", "w")
    file1.write("我是测试数据")
except IOError:
    print("写入异常")
else:
    print("写入成功")
    file1.closed
